/*
 * Copyright (c) Huawei Technologies Co., Ltd. 2021-2022. All rights reserved.
 */

import { FileUtil } from '../utils/file-util.js';

import { BaseCommandBuilder } from './base-command-builder.js';

export class NodeCommandBuilder extends BaseCommandBuilder {
  private _supportNewEnvArgs: boolean;
  private _envArgs: string[];

  constructor(supportNewEnvArgs?: boolean) {
    super([]);
    this._supportNewEnvArgs = supportNewEnvArgs ? supportNewEnvArgs : false;
    this._envArgs = [];
    this.commandList.push(process.execPath);
  }

  extendBuilder(nodeCommandBuilder: NodeCommandBuilder): NodeCommandBuilder {
    this.addAllParams(nodeCommandBuilder.commandList);
    this._supportNewEnvArgs = nodeCommandBuilder.isEtsLoader();
    this._envArgs = nodeCommandBuilder.getEnvArgs();
    return this;
  }

  addWebpackPath(webpackPath: string): NodeCommandBuilder {
    FileUtil.checkPathLength(webpackPath);
    this.commandList.push(webpackPath);
    return this;
  }

  addJsPath(jsPath: string): NodeCommandBuilder {
    FileUtil.checkPathLength(jsPath);
    this.commandList.push(jsPath);
    return this;
  }

  getTsVersion(): NodeCommandBuilder {
    this.commandList.push('--bc-version');
    return this;
  }

  getCompatibleTsVersion(compatibleSdkVersion: number): NodeCommandBuilder {
    this.commandList.push('--target-api-version', compatibleSdkVersion.toString());
    return this;
  }

  getCompatibleSdkVersionStage(compatibleSdkVersionStage: string): NodeCommandBuilder {
    this.commandList.push('--target-api-sub-version', compatibleSdkVersionStage);
    return this;
  }

  addWebpackConfig(config: string): NodeCommandBuilder {
    this.commandList.push('--config');
    this.commandList.push(config);
    return this;
  }

  addTitle(title: string): NodeCommandBuilder {
    this.commandList.push(`--title=${title}`);
    return this;
  }

  addObfuscateType(obfuscateType: string): NodeCommandBuilder {
    this.addEnvArgs('obfuscate', obfuscateType);
    return this;
  }

  addDeviceType(deviceTypeList: string[]): NodeCommandBuilder {
    const deviceTypes = deviceTypeList.join(',');
    if (this._supportNewEnvArgs) {
      this.addEnvArgs('deviceType', deviceTypes);
    } else {
      this.commandList.push('--deviceType');
      this.commandList.push(deviceTypes);
    }
    return this;
  }

  addBuildMode(isDebug: boolean): NodeCommandBuilder {
    const buildMode: string = isDebug ? 'debug' : 'release';
    if (this._supportNewEnvArgs) {
      this.addEnvArgs('buildMode', buildMode);
    } else {
      this.commandList.push('--buildMode');
      this.commandList.push(buildMode);
    }
    return this;
  }

  addCompilerType(compilerType: string): NodeCommandBuilder {
    if (this._supportNewEnvArgs) {
      this.addEnvArgs('compilerType', compilerType);
    } else {
      this.commandList.push('--compilerType');
      this.commandList.push(compilerType);
    }
    return this;
  }

  addOpenSslLegacyOption(flag: boolean): NodeCommandBuilder {
    if (flag) {
      this.commandList.push('--openssl-legacy-provider');
    }
    return this;
  }

  //  Override
  build(): string[] {
    this.appendEnvArgs();
    return super.build();
  }

  isEtsLoader(): boolean {
    return this._supportNewEnvArgs;
  }

  public getEnvArgs(): string[] {
    return this._envArgs;
  }

  private addEnvArgs(key: string, value: string) {
    this._envArgs.push(`${key}=${value}`);
  }

  private appendEnvArgs(): void {
    if (!this._supportNewEnvArgs || this._envArgs.length === 0) {
      return;
    }
    if (this.commandList.indexOf('--env') === -1) {
      this.commandList.push('--env');
    }
    super.commandList = this.commandList.concat(this._envArgs);
    this._envArgs = [];
  }
}
